public class Solution275 {
    public int hIndex(int[] citations) {
        int n = citations.length;
        int l = 0, r = n - 1;
        while (l < r) {
            int m = l + (r - l) / 2;
            if (citations[m] == n - m) {
                return citations[m];
            } else if (citations[m] > n - m) {
                r = m;
            } else {
                l = m + 1;
            }
        }
        int h = n - r;
        return h > citations[r] ? 0 : h;
    }
}
